package com.fick.struts.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fick.hibernate.dao.VoteDAO;
import com.fick.hibernate.dao.VoteinfoDAO;
import com.fick.hibernate.model.Vote;
import com.fick.hibernate.model.Voteinfo;
import com.fick.tools.SystemLogger;
/**
 * 在线调查选项管理
 * add by 王浩强
 * 2009-08-17
 * */
public class VoteinfoAction extends DispatchAction {

	private VoteinfoDAO dao=new VoteinfoDAO();
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id=0;
		int voteid=0;
		String name=request.getParameter("name");
		try {
			voteid=Integer.parseInt(request.getParameter("voteid"));
				if (request.getParameter("id")!=null && request.getParameter("id").trim().length()>0) {
					id=Integer.parseInt(request.getParameter("id"));
				}
		} catch (Exception e) {
			e.printStackTrace();
			voteid=0;
			id=0;
		}
		Vote vote=null;
		try {
			vote=new VoteDAO().findById(voteid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (id!=0) {
			//修改操作
			Voteinfo voteinfo=dao.findById(id);
			if (voteinfo!=null) {
				voteinfo.setVote(vote);
				voteinfo.setVoteinfoName(name);
				dao.update(voteinfo);
			}
		}else {
			//添加操作
			Voteinfo voteinfo=new Voteinfo();
			voteinfo.setVote(vote);
			voteinfo.setVoteinfoName(name);
			voteinfo.setVoteinfoClickNum(0);
			dao.save(voteinfo);
		}
		response.sendRedirect("/admin/voteinfo.jsp?voteid="+voteid);
		return null;
	}

	public ActionForward delById(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		int voteid=0;
		try {
			voteid=Integer.parseInt(request.getParameter("voteid"));
		} catch (Exception e) {
			e.printStackTrace();
			voteid=0;
		}
		if (id!=null && id.trim().length()>0) {
			Voteinfo voteinfo=dao.findById(Integer.parseInt(id));
			if (voteinfo!=null) {
				dao.delete(voteinfo);
			}
		}
		response.sendRedirect("/admin/voteinfo.jsp?voteid="+voteid);
		return null;
	}
	public ActionForward delAll(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String str = request.getParameter("str");
		int voteid=0;
		try {
			voteid=Integer.parseInt(request.getParameter("voteid"));
		} catch (Exception e) {
			e.printStackTrace();
			voteid=0;
		}
		Voteinfo voteinfo;
		String[] ids = str.split(",");
		int id = 0;
		try {
			for (int i = 0; i < ids.length; i++) {
				id = Integer.parseInt(ids[i]);
				voteinfo = dao.findById(id);
				if(voteinfo!=null){
					dao.delete(voteinfo);
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			log.error(e);
		}
		response.sendRedirect("/admin/voteinfo.jsp?voteid="+voteid);
		return null;
	}
}
